-- ipstat.lua
-- Shows login stats by block
-- Version 1.0
-- By Tyrone

---------------------------------------

--[[
Changelog:
1.0
-Basic functionality
]]



-- set up all command prefixes
cmdNames = {
    "ipstat", "ipstats"
}


COOLDOWN = 120 -- plugin cooldown, 0 for none
NEWLINE = string.format("\n")
_,bot_name = VH:GetConfig("config", "hub_security")

function doCommand(nick, data, z)
	
	if (data == nil) then
		if (time > 0) then
			SendMessageToUser("This command can only be used every "..COOLDOWN.." seconds.  Please wait "..time.."s.",nick,bot_name)
		else
			SendMessageToHub(getStats(), bot_name)
			time = COOLDOWN
		end
	elseif (data == "reload") then
		reload()
	elseif (data == "") then
		if (time > 0) then
			SendMessageToUser("This command can only be used every "..COOLDOWN.." seconds.  Please wait "..time.."s.",nick,bot_name)
		else
			SendMessageToHub(getStats(), bot_name)
			time = COOLDOWN
		end
		time = COOLDOWN
	end
end

function doHelp(nick)
    SendMessageToUser("Usage: ", nick, bot_name)
    DisplayHelpMessage("help", "Shows this message.", nick)
    -- put coad in here for help commands:
	DisplayHelpMessage("", "Displays a list of people online from each block", nick)
	if (UserClass(nick) > 3) then
		DisplayHelpMessage("reload", "Re-iterates over the nicklist to get new stats", nick)
	end

end

-- insert other functions here

function reload()
	bara = 0
	bk = 0
	carn = 0
	kinaba = 0
	girro = 0
	mtg = 0
	wtf = 0
	opbara = 0
	opbk = 0
	opcarn = 0
	opkinaba = 0
	opgirro = 0
	opmtg = 0
	opwtf = 0
	_,nicklist = VH:GetNickList()
	nicklist = string.sub(nicklist, 11)
--	SendMessageToUser(nicklist, "Aethereal", bot_name)
	for nick in string.gmatch(nicklist, "([^%$]+)%$%$") do
--		SendMessageToUser("Nick="..nick.." IP="..UserIP(nick), "Aethereal", bot_name)
		ip = UserIP(nick)
		if (ip == 0) or (ip == "127.0.0.1") then
				--Bot!
				subnet = "Bot"
		elseif (string.sub(ip, 1, 7) ~= "192.168") then
				--crappy IP
				subnet = "Bad"
		else
				subnet = string.sub(ip, 9, 10)
		end
		if (subnet == nil) then
			wtf = wtf + 1
			if (UserClass(nick) > 2) then
				opwtf = opwtf + 1
			end
		elseif (subnet == "61") then
			bara = bara + 1
			if (UserClass(nick) > 2) then
				opbara = opbara + 1
			end
		elseif (subnet == "62") then
			bk = bk + 1
			if (UserClass(nick) > 2) then
				opbk = opbk + 1
			end
		elseif (subnet == "63") then
			carn = carn + 1
			if (UserClass(nick) > 2) then
				opbk = opbk + 1
			end
		elseif (subnet == "64") then
			kinaba = kinaba + 1
			if (UserClass(nick) > 2) then
				opkinaba = opkinaba + 1
			end
		elseif (subnet == "65") then
			girro = girro + 1
			if (UserClass(nick) > 2) then
				opgirro = opgirro + 1
			end
		elseif (subnet == "66") then
			mtg = mtg + 1
			if (UserClass(nick) > 2) then
				opmtg = opmtg + 1
			end
		elseif (subnet == "Bot") then
			--do nothing
		else
			wtf = wtf + 1
			if (UserClass(nick) > 2) then
				opwtf = opwtf + 1
			end
		end
	end
end

function VH_OnNewConn(ip)
	if (ip == 0) or (ip == "127.0.0.1") then
			--Bot!
			subnet = "Bot"
	elseif (string.sub(ip, 1, 7) ~= "192.168") then
			--crappy IP
			subnet = "Bad"
	else
			subnet = string.sub(ip, 9, 10)
	end
	if (subnet == nil) then
		wtf = wtf + 1
		if (UserClass(nick) > 2) then
			opwtf = opwtf + 1
		end
	elseif (subnet == "61") then
		bara = bara + 1
		if (UserClass(nick) > 2) then
			opbara = opbara + 1
		end
	elseif (subnet == "62") then
		bk = bk + 1
		if (UserClass(nick) > 2) then
			opbk = opbk + 1
		end
	elseif (subnet == "63") then
		carn = carn + 1
		if (UserClass(nick) > 2) then
			opbk = opbk + 1
		end
	elseif (subnet == "64") then
		kinaba = kinaba + 1
		if (UserClass(nick) > 2) then
			opkinaba = opkinaba + 1
		end
	elseif (subnet == "65") then
		girro = girro + 1
		if (UserClass(nick) > 2) then
			opgirro = opgirro + 1
		end
	elseif (subnet == "66") then
		mtg = mtg + 1
		if (UserClass(nick) > 2) then
			opmtg = opmtg + 1
		end
	else
		wtf = wtf + 1
		if (UserClass(nick) > 2) then
			opwtf = opwtf + 1
		end
	end
end

function VH_OnCloseConn(ip)
	if (ip == 0) or (ip == "127.0.0.1") then
			--Bot!
			subnet = "Bot"
	elseif (string.sub(ip, 1, 7) ~= "192.168") then
			--crappy IP
			subnet = "Bad"
	else
			subnet = string.sub(ip, 9, 10)
	end
	if (subnet == nil) then
		wtf = wtf - 1
		if (UserClass(nick) > 2) then
			opwtf = opwtf - 1
		end
	elseif (subnet == "61") then
		bara = bara - 1
		if (UserClass(nick) > 2) then
			opbara = opbara - 1
		end
	elseif (subnet == "62") then
		bk = bk - 1
		if (UserClass(nick) > 2) then
			opbk = opbk - 1
		end
	elseif (subnet == "63") then
		carn = carn - 1
		if (UserClass(nick) > 2) then
			opbk = opbk - 1
		end
	elseif (subnet == "64") then
		kinaba = kinaba - 1
		if (UserClass(nick) > 2) then
			opkinaba = opkinaba - 1
		end
	elseif (subnet == "65") then
		girro = girro - 1
		if (UserClass(nick) > 2) then
			opgirro = opgirro - 1
		end
	elseif (subnet == "66") then
		mtg = mtg - 1
		if (UserClass(nick) > 2) then
			opmtg = opmtg - 1
		end
	else
		wtf = wtf - 1
		if (UserClass(nick) > 2) then
			opwtf = opwtf - 1
		end
	end
end

function getStats()
	outstring = "Online User Stats:"..NEWLINE
	outstring = outstring.."Logged in from Barakula: "..bara
	if (opbara > 0) then
		outstring = outstring.." with "..opbara.." operators."..NEWLINE
	else
		outstring = outstring..NEWLINE
	end
	
	outstring = outstring.."Logged in from BK: "..bk
	if (opbk > 0) then
		outstring = outstring.." with "..opbk.." operators."..NEWLINE
	else
		outstring = outstring..NEWLINE
	end
	
	outstring = outstring.."Logged in from Carnarvon: "..carn
	if (opcarn > 0) then
		outstring = outstring.." with "..opcarn.." operators."..NEWLINE
	else
		outstring = outstring..NEWLINE
	end
	
	outstring = outstring.."Logged in from Kinaba: "..kinaba
	if (opkinaba > 0) then
		outstring = outstring.." with "..opkinaba.." operators."..NEWLINE
	else
		outstring = outstring..NEWLINE
	end
	
	outstring = outstring.."Logged in from Girraween: "..girro
	if (opgirro > 0) then
		outstring = outstring.." with "..opgirro.." operators."..NEWLINE
	else
		outstring = outstring..NEWLINE
	end
	
	outstring = outstring.."Logged in from Mt. Gravatt: "..mtg
	if (opmtg > 0) then
		outstring = outstring.." with "..opmtg.." operators."
	end
	
	if (wtf > 0) then
		outstring = outstring..NEWLINE.."Logged in from UNKNOWN: "..wtf
	end
	return outstring
end

--192.168.61.x == Barakula
--192.168.62.x == BK
--192.168.63.x == Carnarvon
--192.168.64.x == Kinaba
--192.168.65.x == Girraween
--192.168.66.x == Mt. Gravatt
bara = 0
bk = 0
carn = 0
kinaba = 0
girro = 0
mtg = 0
wtf = 0
opbara = 0
opbk = 0
opcarn = 0
opkinaba = 0
opgirro = 0
opmtg = 0
opwtf = 0
---------------------------------------
-- you can ignore below here
time = 0

function Main()
	reload()
end

function VH_OnParsedMsgChat(nick, data)
	if (string.find(data, "^%."..cmdNames[1])) then
		_,_,cmd = string.find(data, "^%."..cmdNames[1].."%s(.+)")
		if (cmd == "help") then
		    doHelp(nick)
		else
        	doCommand(nick, cmd, cmdNames[1])
        end
    	return 0
	end
	return 1
end

function ready(nick)
    if (time == 0) then
		time = COOLDOWN
	elseif (time == nil) then
		time = COOLDOWN
	else
		SendMessageToUser("This command can only be used every "..COOLDOWN.." seconds.  You have "..time.." seconds to wait.", nick, bot_name)
		return true
	end
	return false
end

function SendMessageToHub(data, from)
	return VH:SendDataToAll("<"..from.."> "..data.."|",0,10)
end

function SendMessageToUser(data, nick, from)
	return VH:SendDataToUser("<"..from.."> "..data.."|", nick)
end

function SendPrivateMessageToUser(user, data, from)
    return VH:SendDataToUser("$To: "..user.." From: "..from.." $<"..from.."> "..data.."|", user);
end

function DisplayHelpMessage(cmd, msg, nick)
    SendMessageToUser("        ."..cmdNames[1].." "..cmd, nick, bot_name)
    SendMessageToUser("        "..msg, nick, bot_name)
end

function UserIsOnline(nick)
	result, sIP = VH:GetUserIP(nick)
	if (string.len(sIP) < 7) then
		return false
	else
		return true
	end
end

function UserIP(nick)
	result, sIP = VH:GetUserIP(nick)
	if (string.len(sIP) < 7) then
		return 0
	else
		return sIP
	end
end

function UserClass(nick)
	res, class = VH:GetUserClass(nick)
	if res and class then return class else return 0 end
end

function SendEmoteToHub(data, from)
	result, err = VH:SendDataToAll("<"..from.."> ".."/me "..data.."|",0,10)
	return 1
end

function VH_OnTimer()
	if (time > 0) then
		time = time - 1
	end
	return 1
end